#coding=utf-8

require "aio/core"

class Aio::Module::Cmd::H3C::DisplayCpu < Aio::Module::Cmd::H3C
  include Aio::Module

  def initialize
    super({
      :cmd_full		=> "display cpu-usage",
      :cmd_short	=> "dis cpu",
      :author			=> "Elin",
      :description => "This is H3C Command# display cpu-usage",
      :ranking		=> Ranking_1,
      :platform		=> "all",
      :benchmark	=> {
        :seconds_5  => ["<", 50],
        :minute_1   => ["<", 50],
        :minutes_5  => ["<", 50],
      },
    })
  end

  def parse
    cont = self.context.dup
    useful[:cpu] = {}
    loop do 
      cont.readline_range(/(Slot|Unit) \d?\s?(CPU \d+\s+)?CPU usage/, reg_blank) do |cont_layer|
        parse_cpu(cont_layer)
      end
    end
    useful[:cpu]
  end

  def parse_cpu(context)
    cpu = {}
    useful[:cpu] = cpu
    context.readline_match_block(/(Slot|Unit) (?<slot_id>\d?)\s?(CPU \d+\s+)?CPU/) do |block|
      # 目前只保留 slot 0 的CPU使用率
      #return if block[:slot_id].to_s != '0'
      # 有可能没有 slot 项
      if block[:slot_id].empty?
        block.update(cpu, :slot_id, '0')
      else
        block.update(cpu, :slot_id)
      end
    end
    context.readline_match_block(/(?<seconds_5>\d+)% in last 5 seconds/) do |block|
      block.warning_serious(cpu, :seconds_5, self)
    end
    context.readline_match_block(/(?<minute_1>\d+)% in last 1 minute/) do |block|
      block.warning_serious(cpu, :minute_1, self)
    end
    context.readline_match_block(/(?<minutes_5>\d+)% in last 5 minutes/) do |block|
      block.warning_serious(cpu, :minutes_5, self)
    end
  end
end

